using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class SentRecord
    {
        public bool Exsits(int MsgID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@MsgID",SqlDbType.Int)
            };
            parameters[0].Value = MsgID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SentRecord_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(SentRecordInfo SentRecord)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TaskID",SqlDbType.Int),
                new SqlParameter("@SplitIndex",SqlDbType.Int),
                new SqlParameter("@DestTel",SqlDbType.VarChar,20),
                new SqlParameter("@Content",SqlDbType.VarChar,500),
                new SqlParameter("@SentStatus",SqlDbType.Int),
                new SqlParameter("@SentTime",SqlDbType.DateTime),
                new SqlParameter("@CommPort",SqlDbType.Int)
            };
            parameters[0].Value = SentRecord.TaskID;
            parameters[1].Value = SentRecord.SplitIndex;
            parameters[2].Value = SentRecord.DestTel;
            parameters[3].Value = SentRecord.Content;
            parameters[4].Value = SentRecord.SentStatus;
            parameters[5].Value = SentRecord.SentTime;
            parameters[6].Value = SentRecord.CommPort;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SentRecord_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(SentRecordInfo SentRecord)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@MsgID",SqlDbType.Int),
                new SqlParameter("@TaskID",SqlDbType.Int),
                new SqlParameter("@SplitIndex",SqlDbType.Int),
                new SqlParameter("@DestTel",SqlDbType.VarChar,20),
                new SqlParameter("@Content",SqlDbType.VarChar,500),
                new SqlParameter("@SentStatus",SqlDbType.Int),
                new SqlParameter("@SentTime",SqlDbType.DateTime),
                new SqlParameter("@CommPort",SqlDbType.Int)
            };
            parameters[0].Value = SentRecord.MsgID;
            parameters[1].Value = SentRecord.TaskID;
            parameters[2].Value = SentRecord.SplitIndex;
            parameters[3].Value = SentRecord.DestTel;
            parameters[4].Value = SentRecord.Content;
            parameters[5].Value = SentRecord.SentStatus;
            parameters[6].Value = SentRecord.SentTime;
            parameters[7].Value = SentRecord.CommPort;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SentRecord_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int MsgID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@MsgID",SqlDbType.Int)
            };
            parameters[0].Value = MsgID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SentRecord_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public SentRecordInfo GetSentRecord(int MsgID)
        {
            SentRecordInfo SentRecord = null;
            SqlParameter[] parameters = {
                new SqlParameter("@MsgID",SqlDbType.Int)
            };
            parameters[0].Value = MsgID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SentRecord_GetDetail", parameters);
                if (dr.Read())
                {
                    SentRecord = new SentRecordInfo();
                    SentRecord.MsgID = dr.GetInt32(0);
                    SentRecord.TaskID = dr.GetInt32(1);
                    SentRecord.SplitIndex = dr.GetInt32(2);
                    SentRecord.DestTel = dr.GetString(3);
                    SentRecord.Content = dr.GetString(4);
                    SentRecord.SentStatus = dr.GetInt32(5);
                    SentRecord.SentTime = dr.GetDateTime(6);
                    SentRecord.CommPort = dr.GetInt32(7);
                }
                return SentRecord;
            }
        }
        public List<SentRecordInfo> GetList(string StrWhere)
        {
            List<SentRecordInfo> SentRecords = new List<SentRecordInfo>();
            SentRecordInfo SentRecord = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SentRecord_GetList", parameters);
                while (dr.Read())
                {
                    SentRecord = new SentRecordInfo();
                    SentRecord.MsgID = dr.GetInt32(0);
                    SentRecord.TaskID = dr.GetInt32(1);
                    SentRecord.SplitIndex = dr.GetInt32(2);
                    SentRecord.DestTel = dr.GetString(3);
                    SentRecord.Content = dr.GetString(4);
                    SentRecord.SentStatus = dr.GetInt32(5);
                    SentRecord.SentTime = dr.GetDateTime(6);
                    SentRecord.CommPort = dr.GetInt32(7);
                    SentRecords.Add(SentRecord);
                }
                return SentRecords;
            }
        }

    }
}